package cn.wolfcode.crm.web.controller;

import cn.wolfcode.crm.domain.SystemDictionary;
import cn.wolfcode.crm.qo.QueryObject;
import cn.wolfcode.crm.service.ISystemDictionaryService;
import cn.wolfcode.crm.util.JsonResult;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/systemDictionary")
public class SystemDictionaryController {

    @Autowired
    private ISystemDictionaryService systemDictionaryService;


    // 字典目录新增或修改
    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    @RequiresPermissions(value={"systemDictionary:saveOrUpdate","字典目录新增或修改"},logical= Logical.OR)
    public JsonResult saveOrUpdate(SystemDictionary systemDictionary){
        try{
            if(systemDictionary.getId() != null){
                systemDictionaryService.update(systemDictionary);
            }else {
                systemDictionaryService.save(systemDictionary);
            }
        }catch (Exception e){
            e.printStackTrace();
            return new JsonResult(false,"操作失败");
        }
        return new JsonResult(true,"操作成功!!!"); // localhost/systemDictionary/list.do
    }

    // 查询所有字典目录数据
    @RequestMapping("/delete")
    @ResponseBody
    @RequiresPermissions(value={"systemDictionary:delete","字典目录删除"},logical= Logical.OR)
    public JsonResult delete(Long id){
        try{
            systemDictionaryService.delete(id);
        }catch (Exception e){
            e.printStackTrace();
            return new JsonResult(false,"删除失败");
        }
        return new JsonResult(true,"删除成功"); // localhost/systemDictionary/list.do
    }

    // 查询所有字典目录数据
    @RequestMapping("/list")
    @RequiresPermissions(value={"systemDictionary:list","字典目录列表"},logical= Logical.OR)
    public String list(Model model, @ModelAttribute("qo")QueryObject qo){
        PageInfo query = systemDictionaryService.query(qo);
        model.addAttribute("pageResult", query);
        return "systemDictionary/list";
    }
}
